﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Threading;
using InSimDotNet;
using InSimDotNet.Packets;
using InSimDotNet.Out;
using System.Collections;
using System.Text.RegularExpressions;
using System.Timers;
using System.Configuration;

namespace lfsserver6
{

   public class parsers
    {
        //public arraylist for button data
        public ArrayList arraylist = new ArrayList();
      


        //Triggered when player joins to the server
        public void player_join_data(InSim insim, IS_NCN iscpr)
        {
            ip_parser();
        }

        //Triggered when player joins to the race.
        public void player_connection_data(InSim insim, IS_NPL isnpl)
        {
            //ip_parser();
        }

        private static string strLogFile = "ipLog.log";
     
        //LOGGER
        public void writeToLogFile(string logMessage)
        {
            //Console.WriteLine("inside write log file");
            string strLogMessage = string.Empty;

            
            StreamWriter swLog;

            strLogMessage = string.Format("{0}", logMessage);

            if (!File.Exists(strLogFile))
            {
                swLog = new StreamWriter(strLogFile);
                swLog.WriteLine("RIBANOZ SOFTWORKS PRESENTS: TOM-IP-LOGGER-2013");
                swLog.WriteLine();
            }
            else
            {
                swLog = File.AppendText(strLogFile);

            }

            swLog.WriteLine(strLogMessage);
            swLog.WriteLine();

            swLog.Close();
        }

       
        public virtual void ip_parser()
        {
            //MUCH MUCH better parser
            using (StreamReader sourcetext = new StreamReader(conf.filename))
            {
                List<string> lines = new List<string>();
                string line;//line to parse
                if (sourcetext == null)
                    throw new ArgumentNullException("Source not found");
                //deletes the old file and generates new iplog file
                File.Delete("ipLog.log");
               while ((line = sourcetext.ReadLine()) != null)
                {

                    //selects line by authorisation
                    string sLine = "Authorised";
                    if (Regex.IsMatch(line, sLine, RegexOptions.IgnoreCase))
                    {
                        //write to log.should skip after first time!
                        //writeToLogFile(line);
                        lines.Add(line);

                    }

                }
                File.WriteAllLines(strLogFile,lines);
            }


        }
       
        //data reader function. Reads data from generated ipLOG file and returns specified user ip data
        public void data_reader(string username)
        {
            using (StreamReader logtext = new StreamReader("ipLog.log"))
            {
                string line;
                //string linetoread;
                if (logtext == null)
                    throw new ArgumentNullException("Source not found");
                while ((line = logtext.ReadLine()) != null)
                {
                    if (username == "all")
                    {
                            string sLine = "Authorised";
                            if (Regex.IsMatch(line, sLine, RegexOptions.IgnoreCase))
                            {
                                arraylist.Add(line);
                                //set_user_data(linetoread);
                                //Console.WriteLine(linetoread);
                            }

                    }

                    else if (Regex.IsMatch(line, username, RegexOptions.IgnoreCase))
                    {
                        bool comp = ExactMatch(line, username);
                        if (comp == true)
                        {
                            arraylist.Add(line);
                            //set_user_data(line);
                            //Console.WriteLine(line);
                        }
                    }
                }
            }
        }
        //exact regex matcher
         bool ExactMatch(string input, string match)
        {
            return Regex.IsMatch(input, string.Format(@"\b{0}\b", Regex.Escape(match)));
        }
       //only for ip reading
        public void ip_reader(string ip_address)
        {
          
            string line;
            using (StreamReader sr = new StreamReader("ipLog.log"))
            {
                if (sr == null)
                {
                    throw new ArgumentNullException("source not found");
                }
                while((line = sr.ReadLine()) != null)
                {
                    Regex.IsMatch(line, ip_address, RegexOptions.IgnoreCase);
                    bool compare = ExactMatch(line, ip_address);
                    if (compare == true)
                    {
                        arraylist.Add(line);
                    }
                }
            }
        }

    }



}
